package com.wjy.controller;

import com.wjy.common.bean.MQBean;
import com.wjy.common.bean.info.MailInfo;
import com.wjy.common.bean.info.SmsInfo;
import com.wjy.common.mq.pc.Pusher;
import com.wjy.common.mq.ps.Publisher;
import com.wjy.common.result.ResultBuilder;
import com.wjy.common.util.JacksonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ybxxszl
 * @desc 消息队列控制
 * @date 2019/5/4
 */
@RestController
public class MQController {

    @Autowired
    private Pusher pusher;

    @Autowired
    private Publisher publisher;

    @Autowired
    private JacksonUtil jacksonUtil;

    @RequestMapping(value = "/postMsg", method = RequestMethod.POST)
    public ResultBuilder postMsg(@RequestBody MQBean mqBean) {

        int num = 0;
        try {
            String type = mqBean.getType();
            String name = mqBean.getName();
            MailInfo mailInfo = mqBean.getMailInfo();
            SmsInfo smsInfo = mqBean.getSmsInfo();
            if ("pc".equals(type)) {
                String info = jacksonUtil.toString(mailInfo);
                if (name.contains("MAIL")) {
                    num = pusher.push(name, info);
                }
                if (name.contains("SMS")) {
                    num = pusher.push(name, info);
                }
            }
            if ("ps".equals(type)) {
                String info = jacksonUtil.toString(smsInfo);
                if (name.contains("MAIL")) {
                    num = publisher.publish(name, info);
                }
                if (name.contains("SMS")) {
                    num = publisher.publish(name, info);
                }
            }
            return ResultBuilder.success(num);
        } catch (Exception e) {
            return ResultBuilder.error(e);
        }

    }

}
